﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PowerTriggers.ServiceReference;
using System.Management;
using System.Diagnostics;
using System.IO;

namespace PowerTriggers
{
    internal partial class SelectOtherTaskForm : Form
    {
        private IList<TaskProperty> properties;

        private SelectOtherTaskForm()
        {
            InitializeComponent();
        }

        public SelectOtherTaskForm(TaskTypes taskType, IList<TaskProperty> originalProperties)
            : this()
        {
            this.TaskType = taskType;
            this.properties = originalProperties;
        }

        internal TaskTypes TaskType { get; set; }

        internal Task Task { get; private set; }

        private void SelectTasksForm_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.Default;
            if (WcfClientFactory.CurrentServerName != null)
            {
                string msg = string.Format("You are connected to server {0} and this screen requires you to enter the path of the executable file as it would appear to the remote computer.", WcfClientFactory.CurrentServerName);
                MessageBox.Show(this, msg, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.Default;

                string fullPath = textBox1.Text.Trim();
                if (string.IsNullOrEmpty(fullPath))
                {
                    MessageBox.Show(this, "Missing executable path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (textBox1.CanFocus)
                    {
                        textBox1.Focus();
                    }
                }
                else
                {
                    bool okToContinue;

                    okToContinue = IsValidPath(fullPath);
                    if (!okToContinue)
                    {
                        MessageBox.Show(this, "File path contains invalid characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    if (okToContinue)
                    {
                        if (!System.IO.File.Exists(fullPath))
                        {
                            DialogResult result = MessageBox.Show(this, "The specified file path does not exist. Are you sure you want use this file path", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            switch (result)
                            {
                                case System.Windows.Forms.DialogResult.Yes:
                                    okToContinue = true;
                                    break;
                                default:
                                    okToContinue = false;
                                    break;
                            }
                        }
                        else
                        {
                            okToContinue = true;
                        }
                    }

                    if (okToContinue)
                    {
                        Cursor.Current = Cursors.WaitCursor;

                        string fileDescription = GetFileDescription(fullPath);
                        string description;
                        if (string.IsNullOrEmpty(fileDescription))
                        {
                            description = Path.GetFileName(fullPath);
                        }
                        else
                        {
                            description = fileDescription;
                            if (fileDescription.ToLower() != Path.GetFileName(fullPath).ToLower())
                            {
                                description += " (" + Path.GetFileName(fullPath) + ")";
                            }
                        }

                        using (PowerTriggersWcfClient client = WcfClientFactory.OpenWcfClient())
                        {
                            var props = properties.ToList();

                            this.Task = client.CreateTask(this.TaskType, description, fullPath, string.Empty, string.Empty);
                        }

                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else
                    {
                        if (textBox1.CanFocus)
                        {
                            textBox1.Focus();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                this.Task = null;
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Error: " + ex.Message, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool IsValidPath(string fullPath)
        {
            try
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullPath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private string GetFileDescription(string fullPath)
        {
            string filename = System.IO.Path.GetFileName(fullPath);
            string description = null;
            if (IsValidPath(fullPath) && File.Exists(fullPath))
            {
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fullPath);
                if (!string.IsNullOrEmpty(fvi.FileDescription))
                {
                    description = fvi.FileDescription;
                }
            }

            return description;
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                if (WcfClientFactory.CurrentServerName != null)
                {
                    string msg = string.Format("You are connected to server {0} but the following dialog box will only display your local disk structures. if you select a local file then this file must exist in exactly the same path on the {0} server", WcfClientFactory.CurrentServerName);
                    MessageBox.Show(this, msg, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                if (openFileDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    string fullPath = openFileDialog1.FileName;
                    if (!IsValidPath(fullPath))
                    {
                        MessageBox.Show(this, "File path contains invalid characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        textBox1.Text = fullPath;
                        labelFileDescription.Text = GetFileDescription(fullPath);
                    }
                }
            }
            catch (System.Exception ex)
            {
                this.Task = null;
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Error: " + ex.Message, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
